<?

class lego_page_m extends tbl_abstract{
	protected $_table_name = 'lego_pages';
	
	public function getTitle(){ return $this['title']; }
	
    public function getChilds(){
    	$q = new DQ;
    	$q->tables($this->getFullTableName())
    		->where("`entity_name` = 'lego_page' and `entity_id` = '{$this->getId()}' and `deleted`=0");
    		
    	return $q->objects(__CLASS__);
	}
	
	public function isMy(){
        if(!class_exists("Auth")) return false;
		if(!Auth::isAuthorized()) return false;
		$user = User::getCurrentUser();
		if($user->isAdmin()) return true;
        if($this['user_id'] == $user->getId()) return true;
		return false;
	}
	
	public function hasChilds(){
		return (bool)Database::select("select count(*) from {$this->getFullTableName()} 
			where `entity_name`='lego_page' and `entity_id`='{$this->getId()}' and `deleted`=0", true);
	}
	
	public function remove(){
		$this['deleted'] = 1;
		$this->update();
	}
	public function restore(){
		$this['deleted'] = 0;
		$this->update();
	}
	
	public function insert(){
		if($this['entity_id'] == $this->getId() && $this['entity_name'] == 'lego_page') 
			throw new Exception("Имя родителя совпадает с именем");
		parent::insert();
	}
}
?>